package com.zzw.basic.classloader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ObjectPoolFactory {

	private Map<String,Object> objectPool = new HashMap();
	
	private Object createObject(String clazzName) throws ClassNotFoundException, 
										InstantiationException, IllegalAccessException{
		Class<?> clazz = Class.forName(clazzName);
		return clazz.newInstance();
	}

	public void initPool(String fileName) throws Exception{
		FileInputStream fis = new FileInputStream(fileName);
		Properties props = new Properties();
		props.load(fis);
		for(String name:props.stringPropertyNames()){
			objectPool.put(name,createObject(props.getProperty(name)));
		}
	}
	
	private Object getObject(String name) {
		return objectPool.get(name);
	}
	
	public static void main(String[] args) throws Exception {
		ObjectPoolFactory factory = new ObjectPoolFactory();
		factory.initPool("obj.txt");
		System.out.println(factory.getObject("a"));
		System.out.println(factory.getObject("b"));
	}
}
